package com.one.service.impl;

import com.one.dto.PaginationDate;
import com.one.dto.Result;
import com.one.dto.ReturnDate;
import com.one.entity.SysClassEntity;
import com.one.entity.SysCollegeEntity;
import com.one.mapper.SysClassMapper;
import com.one.service.SysClassService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.one.utils.PaginationUtils;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 */
@Service
public class SysClassServiceImpl extends ServiceImpl<SysClassMapper, SysClassEntity> implements SysClassService {
    private final SysClassMapper sysClassMapper;

    public SysClassServiceImpl(SysClassMapper sysClassMapper) {
        this.sysClassMapper = sysClassMapper;
    }

    @Override
    public Result insertClass(SysClassEntity sysClassEntity) {
        return Result.ok().data(sysClassMapper.insertClass(sysClassEntity));
    }

    @Override
    public Result selectClassByCodeAndName(SysClassEntity sysClassEntity) {
        if (sysClassEntity.getCode().equals("") && sysClassEntity.getName().equals("")) {
            return Result.error().message("编号和名称不能都为空");
        }
        return Result.ok().data(sysClassMapper.selectClassByCodeAndName(sysClassEntity));
    }

    @Override
    public Result deleteClass(SysClassEntity sysClassEntity) {
        return Result.ok().data(sysClassMapper.deleteClass(sysClassEntity));
    }

    @Override
    public Result updateClass(SysClassEntity sysClassEntity) {
        return Result.ok().data(sysClassMapper.updateClass(sysClassEntity));
    }

    @Override
    public Result selectClass(SysClassEntity sysClassEntity) {
        int page = sysClassEntity.getPage();
        sysClassEntity.setPage(PaginationUtils.getPageIndex(sysClassEntity.getPage(), sysClassEntity.getNumber()));
        List<Object> result=sysClassMapper.selectClass(sysClassEntity);
        int sum=((List<Integer>) result.get(1)).get(0);
        if (sum == 0) return Result.error().message("查询结果为空");
        PaginationDate paginationDate = PaginationUtils.getPaginationDate(sysClassEntity.getNumber(), page, sum);
        if (PaginationUtils.isValid(paginationDate))
            return Result.error().message("分页参数错误!");
        return Result.ok().data(new ReturnDate(paginationDate,(List<SysClassEntity>) result.get(0) ));
    }
}
